package com.fourth.config;

import com.fourth.task.RoleStateTask;
import org.springframework.context.annotation.Configuration;
import org.quartz.*;
import org.springframework.context.annotation.Bean;

/**
 * @author Mr.ma
 * @since 2023/2/28 14:47
 **/
@Configuration
public class QuartzConfig {
    private static final String LIKE_TASK_IDENTITY = "LikeTaskQuartz";

    @Bean
    public JobDetail quartzDetail() {
        return JobBuilder.newJob(RoleStateTask.class).withIdentity(LIKE_TASK_IDENTITY).storeDurably().build();
    }

    @Bean
    public Trigger quartzTrigger() {
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
//              .withIntervalInSeconds(10)  // 可以设置时间周期单位秒
                // 一般设定为两个小时执行一次，为了方便观察设置5分钟执行一次
                .withIntervalInMinutes(5)
                .repeatForever();
        return TriggerBuilder.newTrigger().forJob(quartzDetail())
                .withIdentity(LIKE_TASK_IDENTITY)
                .withSchedule(scheduleBuilder)
                .build();
    }
}
